package com.clj.Spring_Security_Authentication.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.core.userdetails.User;
import com.clj.Spring_Security_Authentication.entity.UserInfo;

@Configuration
public class UserDetailService implements UserDetailsService {
	@Autowired
    private PasswordEncoder passwordEncoder;
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 模拟一个用户，替代数据库获取逻辑
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(username);
		userInfo.setPassword(this.passwordEncoder.encode("123456"));
		//
		System.out.println(userInfo.getPassword());
		return new User(username, userInfo.getPassword(), userInfo.isEnabled(),
				userInfo.isAccountNonExpired(), userInfo.isCredentialsNonExpired(),
				userInfo.isAccountNonLocked(),  AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
	}

}
